package cz.muni.fi.pa165.sme.dao.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import cz.muni.fi.pa165.sme.dao.api.PersistentEntity;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

/**
 * Entity representing sport activity record.
 *
 * @author jirankova
 */
@Entity
@Table(name = "sport_activity_record")
@NamedQueries({
		@NamedQuery(name = SportActivityRecord.SELECT_BY_USER, query = "SELECT sar from SportActivityRecord sar WHERE sar.user.id = :userId"),
		@NamedQuery(name = SportActivityRecord.SELECT_BY_SPORT_ACTIVITY, query = "select sar from SportActivityRecord sar where sar.sportActivity.id = :sportId"),
		@NamedQuery(name = SportActivityRecord.SELECT_ALL, query = "select sar from SportActivityRecord sar")
})
public class SportActivityRecord implements PersistentEntity {

	public static final String SELECT_BY_USER = "sar.selectByUser";
	public static final String SELECT_BY_SPORT_ACTIVITY = "sar.selectBySportActivity";
	public static final String SELECT_ALL = "sar.selectAll";

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@ManyToOne(targetEntity = SportActivity.class)
	@JoinColumn(name = "id_sport_activity")
	private SportActivity sportActivity;

	@ManyToOne(targetEntity = User.class)
	@JoinColumn(name = "id_user")
	private User user;

	@Column(name = "activity_start")
	@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
	private DateTime activityStart;

	@Column(name = "activity_stop")
	@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
	private DateTime activityStop;

	@Column(name = "distance")
	private Float distance;

	@Column(name = "duration")
	private Float duration;

	public Long getId() {
		return id;
	}

	public void setId(final Long id) {
		this.id = id;
	}

	public SportActivity getSportActivity() {
		return sportActivity;
	}

	public void setSportActivity(final SportActivity sportActivity) {
		this.sportActivity = sportActivity;
	}

	public User getUser() {
		return user;
	}

	public void setUser(final User user) {
		this.user = user;
	}

	public DateTime getActivityStart() {
		return activityStart;
	}

	public void setActivityStart(final DateTime activityStart) {
		this.activityStart = activityStart;
	}

	public DateTime getActivityStop() {
		return activityStop;
	}

	public void setActivityStop(final DateTime activityStop) {
		this.activityStop = activityStop;
	}

	public Float getDistance() {
		return distance;
	}

	public void setDistance(final Float distance) {
		this.distance = distance;
	}

	public Float getDuration() {
		return duration;
	}

	public void setDuration(final Float duration) {
		this.duration = duration;
	}

	@Override
	public boolean equals(final Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		final SportActivityRecord that = (SportActivityRecord) o;


		return !(id == null || (!id.equals(that.id)));
	}

	@Override
	public int hashCode() {
		return id != null ? id.hashCode() : 0;
	}

	@Override
	public String toString() {
		return "SportActivityRecord{" +
				"id=" + id +
				", sportActivity=" + sportActivity +
				", user=" + user +
				", activityStart=" + activityStart +
				", activityStop=" + activityStop +
				", distance=" + distance +
				", duration=" + duration +
				'}';
	}
}
